package com.gack.business.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSSClient;
import com.gack.business.service.BackLogServiceInterface;
import com.gack.business.service.StoreSupportFacityAndServiceServiceInterface;
import com.gack.business.service.StoresServiceInterface;
import com.gack.helper.common.AjaxJson;
import com.gack.helper.common.abstractobj.ApiController;
import com.gack.helper.common.util.OSSUnitHelper;

import io.swagger.annotations.ApiOperation;

/**
 * 
* @ClassName: StoresOptController 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author (ZhangXinYu)  
* @date 2018年6月4日 下午3:04:25 
* @version V1.0
 */
@RestController
@RequestMapping(ApiController.STORE_URL)
public class StoresController {
	
	@Autowired
	private StoresServiceInterface storesService;
	@Autowired
	private StoreSupportFacityAndServiceServiceInterface storeSupportFacityAndService;
	@Autowired
	private BackLogServiceInterface backLogService; 
	
	/**
	 * 
	* @Title: getStores 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param @param searchType	 查询方式  1 门店名称查询  2门店面积查询  3门店使用价格查询
	* @param @param searchKey
	* @param @param currentPage
	* @param @param pageSize
	* @param @return    入参
	* @return AjaxJson    返回类型
	* @author （ZhangXinYu） 
	* @throws
	* @date 2018年6月5日 上午9:03:16 
	* @version V1.0
	 */
	@ApiOperation(value="门店信息查询",notes="门店信息查询")
	@PostMapping("getStores")
	public AjaxJson getStores(String storeid,Integer searchType,String searchKey,Integer currentPage,Integer pageSize){
		AjaxJson ajaxJson = new AjaxJson();
		try{
			ajaxJson.setSuccess(true).setStatus(200).setData(storesService.getStores(storeid,searchType, searchKey, currentPage, pageSize));
		}catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(500).setMsg("门店查询操作失败");
		}
		return ajaxJson;
	}
	
	@ApiOperation(value="查询所有门店信息",notes="分页查询所有门店信息")
	@PostMapping("getAllStores")
	public AjaxJson getAllStores(Integer currentPage,Integer pageSize){
		AjaxJson ajaxJson = new AjaxJson();
		try{
			ajaxJson.setSuccess(true).setStatus(200).setData(storesService.getAllStores(currentPage, pageSize));
		}catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(500).setMsg("门店查询操作失败");
		}
		return ajaxJson;
	}
	
	@ApiOperation(value="查询所有门店",notes="查询所有门店")
	@PostMapping("getAllStoresInfo")
	public AjaxJson getAllStoresInfo(){
		AjaxJson ajaxJson = new AjaxJson();
		ajaxJson.setSuccess(true).setStatus(200).setData(storesService.getAllStores());
		return ajaxJson;
	}
	
	@ApiOperation(value="查询所有未被管理员管理的门店",notes="查询所有未被管理员关联的门店")
	@PostMapping("getAllNoUserStoresInfo")
	public AjaxJson getAllNoUserStoresInfo(String userid){
		AjaxJson ajaxJson = new AjaxJson();
		ajaxJson.setSuccess(true).setStatus(200).setData(storesService.getAllNoUserStores(userid));
		return ajaxJson;
	}
	
	@ApiOperation(value="门店信息导出",notes="门店信息导出")
	@GetMapping("exportStores")
	public void exportStores(HttpServletResponse response,String userid){
		try (OutputStream out = response.getOutputStream(); Workbook wb = storesService.exportStores();) {
			response.setContentType("application/octet-stream;charset=utf-8");  
			String  fileName ="商户信息";
			response.setHeader("Content-Disposition", "attachment;filename*=utf-8'zh_cn'"+URLEncoder.encode(fileName, "UTF-8") + ".xls"); 
			wb.write(out);
			backLogService.addBackLog(userid, "门店信息导出");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@ApiOperation(value="查询门店详情",notes="查询门店详情")
	@PostMapping("getStoresInfo")
	public AjaxJson getStoresInfo(String storesid){
		AjaxJson ajaxJson = new AjaxJson();
		ajaxJson.setSuccess(true).setStatus(200).setData(storesService.getStoresInfo(storesid));
		return ajaxJson;
	}
	
	@ApiOperation(value="添加门店配套设施",notes="添加门店配套设施")
	@PostMapping("addStoreSupportFacity")
	public AjaxJson addStoreSupportFacity(String userid,String facityName){
		AjaxJson ajaxJson = new AjaxJson();
		ajaxJson.setSuccess(true).setStatus(200).setData(storeSupportFacityAndService.AddStoreSupportFacity(userid,facityName));
		return ajaxJson;
	}
	
	@ApiOperation(value="添加门店配套服务",notes="添加门店配套服务")
	@PostMapping("addStoreSupportService")
	public AjaxJson addStoreSupportService(String userid,String serviceName){
		AjaxJson ajaxJson = new AjaxJson();
		ajaxJson.setSuccess(true).setStatus(200).setData(storeSupportFacityAndService.AddStoreSupportService(userid,serviceName));
		return ajaxJson;
	}
	
	@ApiOperation(value="添加多个门店配套设施",notes="添加多个门店配套设施")
	@PostMapping("addStoreSupportFacities")
	public AjaxJson addStoreSupportFacities(String userid,String facityNames){
		AjaxJson ajaxJson = new AjaxJson();
		ajaxJson.setSuccess(true).setStatus(200).setData(storeSupportFacityAndService.addStoreSupportFacities(userid,facityNames));
		return ajaxJson;
	}
	
	@ApiOperation(value="添加多个门店配套服务",notes="添加多个门店配套服务")
	@PostMapping("addStoreSupportServices")
	public AjaxJson addStoreSupportServices(String userid,String serviceNames){
		AjaxJson ajaxJson = new AjaxJson();
		ajaxJson.setSuccess(true).setStatus(200).setData(storeSupportFacityAndService.addStoreSupportServices(userid,serviceNames));
		return ajaxJson;
	}
	
	@ApiOperation(value="获取所有门店配套设施",notes="获取所有门店配套设施")
	@PostMapping("getAllStoreSupportFacities")
	public AjaxJson getAllStoreSupportFacities(){
		AjaxJson ajaxJson = new AjaxJson();
		ajaxJson.setSuccess(true).setStatus(200).setData(storeSupportFacityAndService.getAllFacitys());
		return ajaxJson;
	}
	
	@ApiOperation(value="获取所有门店配套服务",notes="获取所有门店配套服务")
	@PostMapping("getAllStoreSupportServices")
	public AjaxJson getAllStoreSupportServices(){
		AjaxJson ajaxJson = new AjaxJson();
		ajaxJson.setSuccess(true).setStatus(200).setData(storeSupportFacityAndService.getAllServices());
		return ajaxJson;
	}
	
	@ApiOperation(value="获取所有门店配套设施及门店配套服务",notes="获取所有门店配套设施及门店配套服务")
	@PostMapping("getAllStoreSupportFacitiesAndServices")
	public AjaxJson getAllStoreSupportFacititesAndServices(){
		AjaxJson ajaxJson = new AjaxJson();
		ajaxJson.setSuccess(true).setStatus(200).setData(storeSupportFacityAndService.getAllFacitysAndServices());
		return ajaxJson;
	}
	
	@ApiOperation(value="移除门店配套设施",notes="移除门店配套设施")
	@PostMapping("dealStoreSupportFacity")
	public AjaxJson dealStoreSupportFacity(String userid,String facityId){
		AjaxJson ajaxJson = new AjaxJson();
		ajaxJson.setSuccess(true).setStatus(200).setData(storeSupportFacityAndService.dealStoreSupportFacity(userid,facityId));
		return ajaxJson;
	}
	
	@ApiOperation(value="移除门店配套服务",notes="移除门店配套设施")
	@PostMapping("dealStoreSupportService")
	public AjaxJson dealStoreSupportService(String userid,String serviceId){
		AjaxJson ajaxJson = new AjaxJson();
		ajaxJson.setSuccess(true).setStatus(200).setData(storeSupportFacityAndService.dealStoreSupportService(userid,serviceId));
		return ajaxJson;
	}
	
	/**
	 * 
	* @Title: addNewStore 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param @param storeName		//门店名称
	* @param @param storeDeposit	//门店押金
	* @param @param storeArea		//门店面积
	* @param @param supportFacities	//门店配套设施 id
	* @param @param storeService	//门店配套服务 id
	* @param @param startTime		//开始营业时间
	* @param @param endTime			//结束营业时间
	* @param @param storeAddress	//门店详细地址
	* @param @param longitude		//经度
	* @param @param latitude		//维度
	* @param @param storePrice		//门店价格
	* @param @param storeUser		//门店管理者名称
	* @param @param storePhone		//门店管理员电话
	* @param @param storeLandline	//门店座机
	* @param @param storep_hotourl	//门店首页图url
	* @param @param onephotourl		//门店第一张图片url
	* @param @param twophoneurl		//门店第二张图片url
	* @param @param threephotourl	//门店第三张图片url
	* @param @param fourphotourl	//门店第四张图片url
	* @param @param fivephotourl	//门店第五张图片url
	* @param @param sixphotourl		//门店第六张图片url
	* @param @return    入参
	* @return AjaxJson    返回类型
	* @author （ZhangXinYu） 
	* @throws
	* @date 2018年6月5日 下午3:16:13 
	* @version V1.0
	 */
	/**
	 * String storeName,Integer storeDeposit,Integer storeArea,List<String> supportFacities,List<String> storeService,
			Date startTime,Date endTime,String storeAddress,String longitude,String latitude,Integer storePrice,
			String storeUser,String storePhone,String storeLandline,String storep_hotourl,String onephotourl,
			String twophoneurl,String threephotourl,String fourphotourl,String fivephotourl,String sixphotourl
	 */
	@ApiOperation(value="新增门店",notes="新增门店")
	@PostMapping("addNewStore")
//	public AjaxJson addNewStore(NewStoreVO newStoreVO){
	public AjaxJson addNewStore(String userid,String storeName,Integer storeDeposit,Integer storeArea,
			String supportFacities,String storeService,String startTime,String endTime,String storeAddress,
			String longitude,String latitude,Integer storePrice,String storeUser,String storePhone,
			String storeLandline,String storep_hotourl,String onephotourl,String twophoneurl,String threephotourl,
			String fourphotourl,String fivephotourl,String sixphotourl,String province,String city,String area){
		AjaxJson ajaxJson = new AjaxJson();
		try{
			/**
			 * storesService.addNewStore(storeName, storeDeposit, storeArea,
					supportFacities, storeService, startTime, endTime, storeAddress, longitude, latitude, 
					storePrice, storeUser, storePhone, storeLandline, storep_hotourl, onephotourl, twophoneurl, 
					threephotourl, fourphotourl, fivephotourl, sixphotourl
			 */
			ajaxJson.setSuccess(true).setStatus(200).setData(storesService.addNewStore(userid, storeName,
					storeDeposit, storeArea, supportFacities, storeService, startTime, endTime,
					storeAddress, longitude, latitude, storePrice, storeUser, storePhone, storeLandline,
					storep_hotourl, onephotourl, twophoneurl, threephotourl, fourphotourl, fivephotourl, sixphotourl, province, city, area));
		}catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false).setStatus(500).setMsg("新增门店操作失败");
		}
		return ajaxJson;
	}
	
	@ApiOperation(value="查询编辑时门店详情",notes="查询编辑时门店详情")
	@PostMapping("getEditStoresInfo")
	public AjaxJson getEditStoresInfo(String storeid){
		AjaxJson ajaxJson = new AjaxJson();
		try{
			ajaxJson.setSuccess(true).setStatus(200).setData(storesService.getEditStoresInfo(storeid));
		}catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false).setStatus(500).setMsg("门店查询操作失败");
		}
		return ajaxJson;
	}
	
	@ApiOperation(value="删除门店",notes="删除门店")
	@PostMapping("dealOneStore")
	public AjaxJson dealOneStore(String userid,String storeid){
		AjaxJson ajaxJson = new AjaxJson();
		try{
			ajaxJson.setSuccess(true).setStatus(200).setData(storesService.dealOneStore(userid,storeid));
		}catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false).setStatus(500).setMsg("门店删除操作失败");
		}
		return ajaxJson;
	}
	

	@ApiOperation(value="门店图片上传",notes="门店图片上传")
	@PostMapping("/putStoreImg")
	public AjaxJson putImg(MultipartFile contractlogo , HttpServletResponse response){
		AjaxJson ajaxJson=new AjaxJson();
		if(contractlogo!=null){
			OSSUnitHelper ossunit=new OSSUnitHelper();
			OSSClient client=OSSUnitHelper.getOSSClient();
			String diskName = "datas/video/stores/";
			String fileName = new Date().getTime() + (int) ((Math.random() * 9 + 1) * 100000)
					+ contractlogo.getOriginalFilename().substring(contractlogo.getOriginalFilename().indexOf("."));
	        String md5key = OSSUnitHelper.uploadObject2OSS(client, contractlogo, diskName,fileName);
	        String url="";
	        url=OSSUnitHelper.getUrl(client, diskName, fileName);
	        String urll=    url.substring(4, url.length());
	        url="https"+urll;
	        System.out.println(url+"5");
	        if(url!=null&&!url.equals("")){
	        	ajaxJson.setSuccess(true).setStatus(200).setData(url);
	        	response.setContentType(url);
	        } 
		 }
		 return ajaxJson;
	}
	
	@ApiOperation(value="负责人编辑门店",notes="负责人编辑门店")
	@PostMapping("principalEditStore")
//	public AjaxJson principalEditStore(EditStoreVO editStoreVO){
	public AjaxJson principalEditStore(String userid,String id,String supportFacities,String storeService,String startTime,
			String endTime,Integer storePrice,String storePhone,String storeLandline,String storep_hotourl,String onephotourl,
			String twophoneurl,String threephotourl,String fourphotourl,String fivephotourl,String sixphotourl,Integer isuse,
			String storeUser,Integer storeArea, String storeName){
		AjaxJson ajaxJson = new AjaxJson();
		try{
			ajaxJson.setStatus(200).setSuccess(true).setData(storesService.updateStoreInfo(userid, id, supportFacities,
					storeService, startTime, endTime, storePrice, storePhone, storeLandline, storep_hotourl,
					onephotourl, twophoneurl, threephotourl, fourphotourl, fivephotourl, sixphotourl,isuse,storeUser,storeArea, storeName));
		}catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setStatus(500).setSuccess(false).setData(null).setMsg("负责人编辑门店操作失败");
		}
		return ajaxJson;
	}
	
	@ApiOperation(value="查看门店使用详情",notes="查看门店使用详情")
	@PostMapping("getStoreUseInfo")
	public AjaxJson getStoreUseInfo(String storeid,String orderid){
		AjaxJson ajaxJson = new AjaxJson();
		try{
			ajaxJson.setStatus(200).setSuccess(true).setData(storesService.getStoreUseInfo(storeid,orderid));
		}catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setStatus(500).setSuccess(false).setData(null).setMsg("查看门店使用详情操作失败");
		}
		return ajaxJson;
	}
	
	@ApiOperation(value="查看门店预约详情",notes="查看门店预约详情")
	@PostMapping("getStoreBookingDetail")
	public AjaxJson getStoreBookingDetail(String storeid,String orderid){
		AjaxJson ajaxJson = new AjaxJson();
		try {
			ajaxJson.setStatus(200).setSuccess(true).setData(storesService.getStoreBookingDetail(storeid,orderid));
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setStatus(500).setSuccess(false).setData(null).setMsg("查看门店预约详情操作失败");
		}
		return ajaxJson;
	}
	
	@ApiOperation(value="查看门店使用列表",notes="查看门店使用列表")
	@PostMapping("getStoreUseInfoList")
	public AjaxJson getStoreUseInfoList(String storeid,String beginTime,String endTime,Integer page,Integer size){
		AjaxJson ajaxJson = new AjaxJson();
		try {
			ajaxJson.setStatus(200).setSuccess(true).setData(storesService.getStoreUseInfoList(storeid,beginTime, endTime, page, size));
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setStatus(500).setSuccess(false).setData(null).setMsg("查看门店预约详情操作失败");
		}
		return ajaxJson;
	}
	
	@ApiOperation(value="查看门店预约列表",notes="查看门店预约列表")
	@PostMapping("getStoreReservationList")
	public AjaxJson getStoreReservationList(String storeid,String beginTime,String endTime,Integer page,Integer size){
		AjaxJson ajaxJson = new AjaxJson();
		try {
			ajaxJson.setStatus(200).setSuccess(true).setData(storesService.getStoreReservationList(storeid,beginTime, endTime, page, size));
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setStatus(500).setSuccess(false).setData(null).setMsg("查看门店预约详情操作失败");
		}
		return ajaxJson;
	}
	
	/**
	 * type null 无条件查询  0 时间段查询   1 前一周收入  2前一月收入 3前一季度收入 4前一年收入
	 */
	@ApiOperation(value="查询门店收入",notes="查询门店收入")
	@PostMapping("getStoreRevenue")
	public AjaxJson getStoreRevenue(Integer type,String province ,String city,String area,String beginTime,String endTime,Integer page,Integer size){
		AjaxJson ajaxJson = new AjaxJson();
		try {
			ajaxJson.setStatus(200).setSuccess(true).setData(storesService.getStoreRevenue(type,province,city,area,beginTime, endTime,page,size));
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setStatus(500).setSuccess(false).setData(null).setMsg("查看门店预约详情操作失败");
		}
		return ajaxJson;
	}

}
